package com.zycfc.zsf.boot.mybatis.generator.codegen.db.impl;

import com.zycfc.zsf.boot.mybatis.generator.codegen.exception.*;
import java.util.*;
import com.zycfc.zsf.boot.mybatis.generator.codegen.model.*;
import com.zycfc.zsf.boot.mybatis.generator.codegen.db.*;
import java.sql.*;
import com.zycfc.zsf.boot.mybatis.generator.codegen.util.*;
import com.zycfc.zsf.boot.util.text.*;
import org.slf4j.*;

public class MySqlHelper implements IDbHelper
{
    private static final Logger log;
    String sqlColumns;
    String sqlComment;
    String sqlAllTable;
    private String url;
    private String username;
    private String password;
    
    public MySqlHelper() throws CodegenException {
        this.sqlColumns = "select * from information_schema.columns where table_schema=DATABASE() and table_name='%s' ";
        this.sqlComment = "select table_name,table_comment  from information_schema.tables t where t.table_schema=DATABASE() and table_name='%s' ";
        this.sqlAllTable = "select table_name,table_comment from information_schema.tables t where t.table_schema=DATABASE()";
        this.url = "";
        this.username = "";
        this.password = "";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (ClassNotFoundException e) {
            throw new CodegenException("\u627e\u4e0d\u5230mysql\u9a71\u52a8!", e);
        }
    }
    
    @Override
    public void setUrl(final String url, final String username, final String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }
    
    @Override
    public TableModel getByTable(final String tableName) throws CodegenException {
        final TableModel tableModel = this.getTableModel(tableName);
        final List<ColumnModel> colList = this.getColumnsByTable(tableName);
        tableModel.setColumnList(colList);
        return tableModel;
    }
    
    private List<ColumnModel> getColumnsByTable(final String tableName) throws CodegenException {
        final DaoHelper<ColumnModel> dao = new DaoHelper<ColumnModel>(this.url, this.username, this.password);
        final String sql = String.format(this.sqlColumns, tableName);
        final List<ColumnModel> list = dao.queryForList(sql, new MySqlMapCmd());
        return list;
    }
    
    private TableModel getTableModel(final String tableName) throws CodegenException {
        final TableModel tablemodel = new TableModel();
        final DaoHelper<String> dao = new DaoHelper<String>(this.url, this.username, this.password);
        MySqlHelper.log.info(this.url);
        final String sql = String.format(this.sqlComment, tableName);
        MySqlHelper.log.info(sql);
        String comment = dao.queryForObject(sql, new MapCmd<String>() {
            @Override
            public String getObjecFromRs(final ResultSet rs) throws SQLException {
                final String comment = rs.getString("table_comment");
                return comment;
            }
        });
        tablemodel.setTableName(tableName);
        if (StringUtil.isEmpty(comment)) {
            comment = tableName;
        }
        if (comment.startsWith("InnoDB free")) {
            comment = tableName;
            tablemodel.setTabComment(comment);
        }
        else {
            if (comment.indexOf(";") != -1) {
                final int pos = comment.indexOf(";");
                comment = comment.substring(0, pos);
            }
            final String[] aryComment = comment.split("\n");
            tablemodel.setTabComment(aryComment[0]);
        }
        return tablemodel;
    }
    
    @Override
    public List<String> getAllTable(final String tableName) throws CodegenException {
        final DaoHelper<String> dao = new DaoHelper<String>(this.url, this.username, this.password);
        if (!StringUtils.isEmpty(tableName)) {
            this.sqlAllTable = this.sqlAllTable + " AND  table_name like '%" + tableName + "%'";
        }
        final List<String> list = dao.queryForList(this.sqlAllTable, new MapCmd<String>() {
            @Override
            public String getObjecFromRs(final ResultSet rs) throws SQLException {
                return rs.getString("table_name");
            }
        });
        return list;
    }
    
    @Override
    public List<String> getAllTable() throws CodegenException {
        return this.getAllTable(null);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)MySqlHelper.class);
    }
}
